// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIMarketBook.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui instrument.
    /// </summary>
    public partial class UIMarketBook : UIControl
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIMarketBook"/> class. 
        /// Initializes a new instance of the <see cref="UIInstrument"/> class.
        /// </summary>
        public UIMarketBook()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIMarketBook"/> class. 
        /// Initializes a new instance of the <see cref="UIInstrument"/> class.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        public UIMarketBook(IInstrument i)
        {
            this.Instrument = i;

            this.InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Instrument.
        /// </summary>
        public IInstrument Instrument { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The contributions tooltip.
        /// </summary>
        /// <param name="contributions">
        /// The contributions.
        /// </param>
        /// <returns>
        /// The contributions tooltip.
        /// </returns>
        private static string ContributionsTooltip(IContribution[] contributions)
        {
            if (contributions == null)
            {
                return "No Contribution Informations";
            }

            var s = "Contributors";
            foreach (var c in contributions)
            {
                s = s + string.Format("\n{0}\t{1}", c.Contributor, ForgePrettyPrinter.Sizes.Format(c.Size));
            }

            return s;
        }

        /// <summary>
        /// The marketbookGrid_ ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnMarketbookGridUIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            var bidCell = this.Instrument.MarketBook.Bids[e.RowIndex];
            var askCell = this.Instrument.MarketBook.Asks[e.RowIndex];

            // According the column display required data
            switch (e.ColumnIndex)
            {
                case 0:
                    if (bidCell.HasValue)
                    {
                        var value = e.Value;
                        e.Value = ForgePrettyPrinter.Sizes.Format(bidCell.Size);
                        if (value == null || !value.Equals(e.Value))
                        {
                            e.ToolTip = ContributionsTooltip(bidCell.Contributions);
                        }
                    }
                    else
                    {
                        e.Value = ForgePrettyPrinter.Sizes.Format(0);
                        e.ToolTip = string.Empty;
                    }

                    break;

                case 1:
                    if (bidCell.HasValue)
                    {
                        e.Value = ForgePrettyPrinter.Prices.Format(bidCell.Price, this.Instrument.InstrumentStaticData.TickSizeRule.Tick(bidCell.Price));
                    }
                    else
                    {
                        e.Value = "-";
                    }

                    break;

                case 2:
                    if (askCell.HasValue)
                    {
                        e.Value = ForgePrettyPrinter.Prices.Format(askCell.Price, this.Instrument.InstrumentStaticData.TickSizeRule.Tick(askCell.Price));
                    }
                    else
                    {
                        e.Value = "-";
                    }

                    break;

                case 3:
                    if (askCell.HasValue)
                    {
                        var value = e.Value;
                        e.Value = ForgePrettyPrinter.Sizes.Format(askCell.Size);
                        if (value == null || ! value.Equals(e.Value))
                        {
                            e.ToolTip = ContributionsTooltip(askCell.Contributions);
                        }
                    }
                    else
                    {
                        e.Value = ForgePrettyPrinter.Sizes.Format(0);
                        e.ToolTip = string.Empty;
                    }

                    break;
            }
        }

        /// <summary>
        /// The marketbookGrid_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnMarketbookGridUIGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            e.RowCount = this.Instrument.MarketBook.Depth;
        }

        #endregion
    }
}